package com.orcwar.gui.views;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.DragEvent;
import android.view.View;
import android.view.View.OnDragListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orcwar.R;
import com.orcwar.engine.randomizer.OWOrcNameRandomizer;
import com.orcwar.enums.OWUnitClass;
import com.orcwar.enums.SpriteEnum;

/**
 * Custom view that will be used to display information about a character in a
 * character slot.
 * 
 * @author baaleze
 * 
 */
public class OWCharacterInfoView extends LinearLayout implements OnDragListener {

	private OWUnitClass unitClass;

	public OWCharacterInfoView(Context context, AttributeSet attr) {
		super(context);

		setOnDragListener(this);
	}

	@Override
	public boolean onDrag(View v, DragEvent event) {
		switch (event.getAction()) {
		case DragEvent.ACTION_DRAG_STARTED:

			try {
				OWUnitClass.valueOf(event.getClipDescription().getLabel()
						.toString());
				setBackgroundColor(Color.BLUE);
			} catch (IllegalArgumentException e) {
				// not a unit class!
				setBackgroundColor(Color.RED);
			}
			return true;

		case DragEvent.ACTION_DRAG_ENDED:
			setBackgroundColor(Color.TRANSPARENT);
			return true;

		case DragEvent.ACTION_DRAG_ENTERED:
			setBackgroundColor(Color.GREEN);
			return true;

		case DragEvent.ACTION_DRAG_EXITED:
			setBackgroundColor(Color.BLUE);
			return true;

		case DragEvent.ACTION_DROP:
			// DROPPED
			unitClass = OWUnitClass.valueOf(event.getClipData()
					.getDescription().getLabel().toString());
			update();
			return true;

		case DragEvent.ACTION_DRAG_LOCATION:
			return true;

		default:
			return false;
		}
	}

	private void update() {
		TextView unitName = (TextView) findViewById(R.id.unitName);
		ImageView unitSprite = (ImageView) findViewById(R.id.unitSprite);
		unitName.setText(OWOrcNameRandomizer.generateName());

		switch (unitClass) {
		case BOUCLIEUR:
			unitSprite.setImageBitmap(SpriteEnum.UNIT_BOUCLIEUR.getBitmap());
			break;

		case FLEAUTEUR:
			unitSprite.setImageBitmap(SpriteEnum.UNIT_FLEAUTEUR.getBitmap());
			break;

		case HACHEUR:
			unitSprite.setImageBitmap(SpriteEnum.UNIT_HACHEUR.getBitmap());
			break;

		case HOMMELANCE:
			unitSprite.setImageBitmap(SpriteEnum.UNIT_HOMMELANCE.getBitmap());
			break;

		case TIREUR:
			unitSprite.setImageBitmap(SpriteEnum.UNIT_TIREURDELOIN.getBitmap());
			break;

		default:
			break;
		}
		this.invalidate();

	}

}
